<?php

class log extends CActiveRecord {
/**
 * The followings are the available columns in table 'packing_list':
 * @var integer $id
 * @var integer $shipping_id
 * @var integer $date
 * @var string $subject
 * @var string $ref_no
 * @var string $pod
 */

/**
 * Returns the static model of the specified AR class.
 * @return CActiveRecord the static model class
 */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'log';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
        return array(
            array('desc_id, date, id, table_id, table_ref', 'numerical', 'integerOnly'=>true),
            array('desc,table_id,table_ref,date','required'),
            array('desc', 'length', 'max'=>254),
        );
    }
    public function test() {
        $criteria = new CDbCriteria;
        $desc = 'testing';
            $criteria->condition = "id=123";
            $db = $this->dbConnection;
            $row = $db->commandBuilder->createInsertCommand('log_desc',array('desc'=>$desc))->query();
            if($row->getRowCount() > 0) {
                $pointer = $row->read();
                echo $pointer['desc'];
            }
    }
    public function makeLog($table_id,$table_ref,$desc) {
        $this->date = time();
        $this->table_id = $table_id;
        $this->table_ref = $table_ref;
        if($this->validate()) {
            $criteria = new CDbCriteria;
            $criteria->condition = "desc like :desc";
            $criteria->params = array(':desc'=>$desc);
            $db = $this->dbConnection;
            $row = $db->commandBuilder->createFindCommand('log_desc',$criteria)->query();
            if($row->getRowCount() <= 0) {
                $db->commandBuilder->createInsertCommand('log_desc',array('desc'=>$desc))->execute();
                $row = $db->commandBuilder->createFindCommand('log_desc',$criteria)->query();
            }
            while($pointer = $row->read()) {
                $this->desc_id = $pointer['id'];
            }
            return $this->save(false);
        }
        
    }
    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
            'description'=>array(self::HAS_ONE,'log_desc','desc_id'),
        );
    }
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
        );
    }
}